﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Mvc;

namespace Sencha.Direct
{
	public class FormActionResult<DataT> : DirectJsonResult
	{
		[DataContract]
		public class JsFormResponse
		{
			[DataMember(Name = "success")]
			public bool Success { get; set; }
			[DataMember(Name = "data")]
			public DataT Data { get; set; }
			[DataMember(Name = "errors")]
            public IDictionary<string, string> Errors { get; set; }

            public JsFormResponse(bool success, DataT data, IDictionary<string, string> errors)
			{
				this.Success = success;
				this.Data = data;
				this.Errors = errors;
			}

			public override string ToString()
			{
				return string.Format("form(success: {0})", Success);
			}
		}

		new public JsFormResponse Data { get { return (JsFormResponse)base.Data; } set { base.Data = value; } }
		public override Type DataType { get { return typeof(JsFormResponse); } }

		public FormActionResult()
		{
			this.Data = new JsFormResponse(true, default(DataT), null);
		}
        public FormActionResult(IDictionary<string, string> errors)
			: this()
		{
			if (errors == null)
				throw new ArgumentNullException("errors");

            this.Data.Success = false;
            this.Data.Errors = errors;
		}
		public FormActionResult(DataT data)
			: this()
		{
			this.Data.Success = true;
			this.Data.Data = data;
		}

		public override string ToString()
		{
			return Convert.ToString(this.Data);
		}
	}
}
